package com.mango.ch02;

import java.io.DataInput;
import java.io.DataOutput;
import java.io.IOException;

import org.apache.hadoop.io.DoubleWritable;
import org.apache.hadoop.io.LongWritable;
import org.apache.hadoop.io.WritableComparable;

public class NaturalValue implements WritableComparable<NaturalValue> {
	private LongWritable timestamp =new LongWritable() ;
	private DoubleWritable price =new DoubleWritable();

	public NaturalValue(LongWritable timestamp, DoubleWritable price) {
		this.timestamp = timestamp;
		this.price = price;
	}

	public NaturalValue() {

	}

	@Override
	public String toString() {
		return "NaturalValue [timestamp=" + timestamp + ", price=" + price + "]";
	}

	public LongWritable getTimestamp() {
		return timestamp;
	}

	public void setTimestamp(LongWritable timestamp) {
		this.timestamp = timestamp;
	}

	public DoubleWritable getPrice() {
		return price;
	}

	public void setPrice(DoubleWritable price) {
		this.price = price;
	}

	@Override
	public void write(DataOutput out) throws IOException {
		// TODO Auto-generated method stub
		out.writeLong(this.timestamp.get());
		out.writeDouble(this.price.get());
	}

	@Override
	public void readFields(DataInput in) throws IOException {
		// TODO Auto-generated method stub
		this.timestamp = new LongWritable(in.readLong());
		this.price = new DoubleWritable(in.readDouble());
	}

	@Override
	public int compareTo(NaturalValue arg0) {
		// TODO Auto-generated method stub
		if (this.timestamp.get() != arg0.getTimestamp().get()) {
			return this.timestamp.get() < arg0.getTimestamp().get() ? -1 : 1;
		} else
			return 0;
	}

}
